/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */

package com.ykht.proj.activity.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ykht.proj.activity.Enum.ActivityStatusEnum;
import com.ykht.proj.activity.Enum.PlanStatusEnum;
import com.ykht.proj.activity.constant.CardConstant;
import com.ykht.proj.activity.entity.VActivityCard;
import com.ykht.proj.activity.entity.VDiscountPlan;
import com.ykht.proj.activity.entity.VGroup;
import com.ykht.proj.activity.service.VActivityCardService;
import com.ykht.proj.activity.vo.VActivityVo;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.log.annotation.SysLog;
import com.ykht.proj.activity.entity.VActivity;
import com.ykht.proj.activity.service.VActivityService;
import com.ykht.proj.common.security.util.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;


/**
 * 活动表
 *
 * @author csg
 * @date 2021-05-21 14:20:15
 */
@RestController
@AllArgsConstructor
@RequestMapping("/vactivity")
@Api(value = "vactivity", tags = "活动表管理222222222222")
@Slf4j
public class VActivityController {

	private final VActivityService vActivityService;

	/**
	 * 活动列表 分页查询
	 *
	 * @param page      分页对象
	 * @param vActivityVo 活动表
	 * @return
	 */
	@ApiOperation(value = "活动列表 分页查询", notes = "活动列表 分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('activity_vactivity_view')")
	public R getVActivityPage(Page page, VActivityVo vActivityVo) {
		log.info("->活动列表 分页查询{}{}", page, vActivityVo);
		IPage<VActivityVo> iPage = vActivityService.getVActivityPage(page, vActivityVo);
		return R.ok(iPage);
	}

	@ApiOperation(value = "校验 活动名称")
	@PostMapping("/checkActivityName")
	public R checkGroupName(@RequestBody VActivity vActivity) {
		log.info("->校验 活动名称{}", vActivity);
		VActivity one = vActivityService.getOne(Wrappers.lambdaQuery(VActivity.class)
				.eq(VActivity::getName, vActivity.getName()));
		return R.ok(one == null ? true : one.getId().equals(vActivity.getId()) ? true : false);
	}


	/**
	 * 通过id查询活动表
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('activity_vactivity_view')")
	public R getById(@PathVariable("id") Long id) {
		VActivity activity = vActivityService.getById(id);
		VActivityVo vActivityVo = BeanUtil.toBean(activity, VActivityVo.class);
		return R.ok(vActivityVo);
	}

	/**
	 * 新增活动表
	 *
	 * @param vActivityVo 活动表
	 * @return R
	 */
	@ApiOperation(value = "新增活动表", notes = "新增活动表")
	@SysLog("新增活动表")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('activity_vactivity_add')")
	public R save(@RequestBody VActivityVo vActivityVo) {
		log.info("->新增卡表{}", vActivityVo);
		if (null != vActivityVo.getEndTime() && vActivityVo.getEndTime().isBefore(LocalDateTime.now())) {
			return R.failed("卡片结束时间要大于当前日期");
		}
		if (LocalDateTime.now().isBefore(vActivityVo.getStartTime())) {
			vActivityVo.setStatus(PlanStatusEnum.TO_START.getValue());
		} else {
			vActivityVo.setStatus(PlanStatusEnum.AT_WORK.getValue());
		}
		if (null == vActivityVo.getActivityCardList() || vActivityVo.getActivityCardList().size() == 0) {
			return R.failed("请选择参加活动的卡");
		}
		vActivityService.addActivity(vActivityVo);
		return R.ok();
	}

	/**
	 * 修改活动表
	 *
	 * @param vActivity 活动表
	 * @return R
	 */
	@ApiOperation(value = "修改活动表", notes = "修改活动表")
	@SysLog("修改活动表")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('activity_vactivity_edit')")
	public R updateById(@RequestBody VActivity vActivity) {
		vActivity.setUpdateTime(LocalDateTime.now());
		vActivity.setUpdateUser(SecurityUtils.getUserId());
		vActivityService.updateActivity(vActivity);
		return R.ok();
	}

	/**
	 * 通过id删除活动表
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id删除活动表", notes = "通过id删除活动表")
	@SysLog("通过id删除活动表")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('activity_vactivity_del')")
	public R removeById(@PathVariable Long id) {
		VActivity activity = vActivityService.getById(id);
		if (!ActivityStatusEnum.BE_OVERDUE.getValue().equals(activity.getStatus())){
			return R.failed("该活动没有结束，请先结束该活动");
		}
		vActivityService.deleteActivity(id);
		return R.ok();
	}

}
